package com.learning.jquizenglish.widget;

import com.learning.jquizenglish.R;
import com.learning.jquizenglish.activity.MainActivity;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.ImageView;

public class SquareImageView extends ImageView{

	private int imageScale;
	private int imageWidthScale;
	private int imageHeightScale;
	
	public SquareImageView(Context context) {
		super(context);
	}

	public SquareImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.SquareImageView);
		imageScale = arr.getInteger(R.styleable.SquareImageView_siv_scale, 1);
		imageWidthScale = arr.getInteger(R.styleable.SquareImageView_siv_width_scale, 0);
		imageHeightScale = arr.getInteger(R.styleable.SquareImageView_siv_height_scale, 0);
		arr.recycle();
	}

	public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//		int size = Math.min(getMeasuredWidth(), getMeasuredHeight());
		int width = MainActivity.SCREEN_WIDTH/imageScale;
		int height = MainActivity.SCREEN_WIDTH/imageScale;
		if(imageWidthScale!=0){
			width = MainActivity.SCREEN_WIDTH/imageWidthScale;
		}
		if(imageHeightScale!=0){
			height = MainActivity.SCREEN_WIDTH/imageHeightScale;
		}
		setMeasuredDimension(width, height);
	}
}
